package com.jinzhi.jzapi.training.task;

import com.jinzhi.jzweb.domain.RecruitmentNewsDO;
import com.jinzhi.jzweb.domain.RecruitmentTopDO;
import com.jinzhi.jzweb.service.RecruitmentNewsService;
import com.jinzhi.jzweb.service.RecruitmentTopService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Component
public class AutoTopNewsTimesJob implements Job {

    @Autowired
    private RecruitmentNewsService recruitmentNewsService;
    @Autowired
    private RecruitmentTopService recruitmentTopService;

    @Override
    @Transactional
    public void execute(JobExecutionContext jobExecutionContext){
        log.info("---------------------开始进行置顶有效期计算---------------------");
        //查询所有置顶信息的机构id
        List<Long> orgIds = recruitmentTopService.selOrgIds();
        if(orgIds != null && orgIds.size() > 0){
            Date date = new Date();
            for(Long orgId: orgIds){
                Map map = new HashMap();
                map.put("state",0);
                map.put("orgId",orgId);
                RecruitmentTopDO recruitmentTopDO = recruitmentTopService.selMaxTime(map);
                if(recruitmentTopDO != null){
                    if(recruitmentTopDO.getEndTime().before(date)){
                        List<RecruitmentNewsDO> recruitmentNewsDOS = recruitmentNewsService.findByKv("org_id",orgId);
                        if(recruitmentNewsDOS != null && recruitmentNewsDOS.size() > 0){
                            for(RecruitmentNewsDO recruitmentNewsDO : recruitmentNewsDOS){
                                if(recruitmentNewsDO.getIsTop().equals(1)){
                                    recruitmentNewsDO.setIsTop(0);//变更置顶状态
                                    recruitmentNewsService.updateById(recruitmentNewsDO);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}
